package cn.tom.controller.adm;

import cn.tom.tools.XResp;
import cn.tom.entity.Order;
import cn.tom.tools.XJson;
import cn.tom.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/adm/ord")
public class AdmOrdController {
    @Resource
    private OrderService orderService;
    //管理员 订单管理
    @PostMapping("/findPageOrders2u/{pagenum}/{lines}")
    public XJson findOrdersByPages(Integer payStaus
                                 , Integer shipStatus
                                 , Integer days
                                 , @PathVariable("pagenum")Integer pagenum
                                 , @PathVariable("lines")Integer lines
                                 , String phone
                                 , String account
                                 , String nickname)
    {
        HashMap<String,Object> map = new HashMap<>();
        map.put("phone"       ,phone);
        map.put("account"     ,account);
        map.put("nickname"    ,nickname);
        map.put("payStatus"   ,payStaus);
        map.put("shipStatus"  ,shipStatus);
        map.put("days"        ,days);
        if(pagenum < 1) pagenum = 1;
        if(lines < 5) lines = 5;
        List<Order> ordersByPages =
                orderService.findOrdersByPages(map,pagenum,lines);
        int total = orderService.total(map);
        HashMap<String,Object> mm = new HashMap<>();
        mm.put("orders"   ,ordersByPages );
        mm.put("total"    ,total  );
        return XResp.pack(200,"",mm);
    }


    @GetMapping("/findOrdByUid/{uid}")
    public XJson findOrdByUid(@PathVariable Integer uid){
        Order order = orderService.findOrdByUid(uid);
        return XResp.pack(200,"",order);
    }


    @PostMapping("/updateShip/{ordid}")
    public XJson updateShip(@PathVariable("ordid")Integer ordid
                                                , String shipno
                                                , String shipper)
    {
        int x = orderService.updateShip(ordid,1,shipno,shipper,new Date());
        if(x == 1)
            return
                    XResp.pack(200,"更新发货状态...1，发货成功",null);
        return
                XResp.pack(500,"发货失败！！!",null);
    }
}
